/*
 * CAD System for seamlessly constructing 2D and 3D scenes with aspects and motion
 * Copyright (C) 2013 Bernhard Haumacher and others
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.haumacher.draw5d.math.d2.constructions.internal;

import java.util.List;

import de.haumacher.draw5d.dataflow.Block;
import de.haumacher.draw5d.dataflow.Observable;
import de.haumacher.draw5d.dataflow.Property;
import de.haumacher.draw5d.dataflow.Scope;
import de.haumacher.draw5d.dataflow.core.AbstractObservable;
import de.haumacher.draw5d.dataflow.core.InputImpl;
import de.haumacher.draw5d.dataflow.core.OptionsImpl;
import de.haumacher.draw5d.math.Computation;

/**
 * Base class for computation algorithms.
 * 
 * @author <a href="mailto:haui@haumacher.de">Bernhard Haumacher</a>
 * @version $Revision: 30 $ $Author: haumacher@googlemail.com $ $Date: 2013-11-08 20:37:25 +0000 (Fri, 08 Nov 2013) $
 */
public abstract class ComputationImpl<R> extends AbstractObservable implements Computation<R> {

	private final InputImpl _input;
	private final R _result;

	/**
	 * Creates a {@link ComputationImpl}.
	 * 
	 * @param scope
	 *        See {@link #getScope()}.
	 * @param properties
	 *        The arguments.
	 */
	public ComputationImpl(Scope scope, Property<? extends Observable>... properties) {
		super(scope);

		_input = new InputImpl(this, properties);
		_result = mkResult();
	}

	@Override
	public void foreachDependency(Block<? super Observable> block) {
		_input.foreachDependency(block);
	}

	@Override
	public void addDependency(Observable dependency) {
		_input.addDependency(dependency);
	}

	@Override
	public void removeDependency(Observable dependency) {
		_input.removeDependency(dependency);
	}

	protected abstract R mkResult();

	@Override
	public final R getResult() {
		return _result;
	}

	@Override
	public List<Property<? extends Observable>> getProperties() {
		return _input.getProperties();
	}

	@Override
	public <T extends Observable> T get(Property<T> property) {
		return _input.get(property);
	}

	@Override
	public <T extends Observable> T set(Property<T> property, T value) {
		return _input.set(property, value);
	}

	/**
	 * Creates a {@link Property} of the given type.
	 */
	public static <T> Property<T> property(Class<T> type) {
		return OptionsImpl.property(type);
	}

}
